package com.cx.controller;

import com.cx.pojo.User;
import com.cx.service.UserService;
import com.cx.utils.Pages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public String login(User user, HttpSession session) {
        System.out.println("=====login===============");
        System.out.println("user = " + user);

        User user2 = userService.login(user);
        if (user2 != null) {
            session.setAttribute("user2", user2);

            return "main";
        } else {
//            return "forward:/login.jsp";
            return "redirect:/login.jsp";
        }
    }

    //    查询所有用户信息
    @RequestMapping("/getAllUser")
    public String getAllUser(@RequestParam(defaultValue = "1") Integer currentPage ,
                             @RequestParam(defaultValue = "5") Integer pageSize ,
                             HttpServletRequest request,
                             HttpSession session) {
        System.out.println("=========getAllUser======== ");
        System.out.println("currentPage = " + currentPage);
//        获取当前登录的用户信息
        User user = (User) session.getAttribute("user2");
//        获取角色的关联字段
        Integer rid = user.getRid();
        if (rid==1){

//        创建pages对象
            Pages pages = new Pages();
//        设置当前页
            pages.setCurrentPage(currentPage);
//        设置页面容量
            pages.setPageSize(pageSize);
//        查询总记录数
            int total = userService.count();
//        设置总记录数
            pages.setTotalCount(total);
//        将pages对象保存到request中
            request.setAttribute("pages",pages);
//        分页查询
            List<User> list = userService.getAllUser(currentPage,pageSize);
//        将集合保存到request
            request.setAttribute("list", list);

        }else {//普通用户
            Integer id = user.getId();
            User user2 = userService.getUserById(id);

            List<User> list = new ArrayList<>();
            list.add(user2);
            request.setAttribute("list",list);
        }

        return "userList";
    }

//    根据id删除

    @RequestMapping("/deleteById")
    public String deleteById(Integer id) {
        System.out.println("========deleteById======");
        System.out.println("id = " + id);

//        执行删除
        userService.deleteUserById(id);
//        重定向请求到/user/getAllUser
        return "redirect:/user/getAllUser";

    }

    //    退出
    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        System.out.println("========logout=======");
//    销毁session
        session.invalidate();

//        重定向到login.jsp页面
        return "redirect:/login.jsp";
    }

    //    验证用户名是否存在
    @RequestMapping("/checkName")
    @ResponseBody
    public boolean checkName(String name) {
        System.out.println("=====checkName=========");
        System.out.println("name = " + name);
//        根据用户名查询指定的用户信息
        return userService.myCheck(name);
    }

    @RequestMapping("/addUser")
    public String addUser(User user) {
        System.out.println("=======addUser========");
        System.out.println("user = " + user);
//        执行添加
        userService.addUser(user);

        return "redirect:/user/getAllUser";
    }
//    登陆页面的注册
    @RequestMapping("/register")
    public String register(User user) {
        System.out.println("=======addUser========");
//        登陆页面注册的为普通用户
        user.setRid(2);
        System.out.println("user = " + user);
//        执行添加
        userService.addUser(user);

        return "redirect:/login.jsp";
//        return "login";
    }


    @RequestMapping("/getUserById")
    public String getUserById(Integer id, HttpServletRequest request) {
        System.out.println("=======getUserById========");
        System.out.println("id = " + id);
//        根据id查询指定的用户信息
        User user = userService.getUserById(id);
//        将user对象保存到request中
        request.setAttribute("user", user);
        return "userDetail";
    }

    @RequestMapping("/toEditUser")
    public String toEditUser(Integer id, HttpServletRequest request) {
        System.out.println("=======toEditUser========");
        System.out.println("id = " + id);

//        根据id查询指定的用户信息
        User user = userService.getUserById(id);
//        将user对象保存到request中
        request.setAttribute("user", user);

        return "editUser";
    }


    @RequestMapping("/updateUser")
    public String updateUser(User user) {
        System.out.println("=======updateUser========");
        System.out.println("user = " + user);

//        执行修改
        userService.updateUser(user);

        return "redirect:/user/getAllUser";
    }



}
